define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'prl/channels/index',
                    add_url: 'prl/channels/add',
                    edit_url: 'prl/channels/edit',
                    del_url: 'prl/channels/del',
                    multi_url: 'prl/channels/multi',
                    table: 'prl_channels',
                    dragsort_url: 'prl/channels/weigh',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                sortOrder: 'asc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3'),"4":__('Type 4'),"5":__('Type 5'),"6":__('Type 6')}, formatter: Table.api.formatter.normal},
                        {field: 'name', title: __('Name')},
                        {field: 'en_name', title: __('En_name')},
                        {field: 'weigh', title: __('Weigh')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {
                            field: 'operate', title: __('Operate')
                            , table: table
                            , events: Table.api.events.operate
                            , formatter: Table.api.formatter.operate
                            , buttons: [
                                {
                                    name: '首页配置'
                                    , text: __('首页配置')
                                    , title: __('首页配置')
                                    , classname: 'btn btn-xs btn-warning btn-addtabs'
                                    , url: 'prl/channels/viewsconfig/type/{type}'
                                    , visible: function (row) {
                                        if(row.type == '0')
                                            return true;
                                        return false;
                                    }
                                },
                                {
                                    name: '栏目配置'
                                    , text: __('栏目配置')
                                    , title: __('栏目配置')
                                    , classname: 'btn btn-xs btn-primary btn-addtabs'
                                    , url: 'prl/channels/viewsconfig/type/{type}'
                                    , visible: function (row) {
                                        if(row.type == '1')
                                            return true;
                                        return false;
                                    }
                                },
                                {
                                    name: '列表配置'
                                    , text: __('列表配置')
                                    , title: __('列表配置')
                                    , classname: 'btn btn-xs btn-success btn-addtabs'
                                    , url: 'prl/channels/viewsconfig/type/{type}'
                                    , visible: function (row) {
                                        if(row.type == '2')
                                            return true;
                                        return false;
                                    }
                                },
                                {
                                    name: '详情配置'
                                    , text: __('详情配置')
                                    , title: __('详情配置')
                                    , classname: 'btn btn-xs btn-success btn-addtabs'
                                    , url: 'prl/channels/viewsconfig/type/3'
                                    , visible: function (row) {
                                        if(row.type == '2')
                                            return true;
                                        return false;
                                    }
                                },
                                {
                                    name: '单页配置'
                                    , text: __('单页配置')
                                    , title: __('单页配置')
                                    , classname: 'btn btn-xs btn-info btn-addtabs'
                                    , url: 'prl/channels/viewsconfig/type/{type}'
                                    , visible: function (row) {
                                        if(row.type == '4')
                                            return true;
                                        return false;
                                    }
                                },
                                {
                                    name: '表单配置'
                                    , text: __('表单配置')
                                    , title: __('表单配置')
                                    , classname: 'btn btn-xs btn-danger btn-addtabs'
                                    , url: 'prl/channels/viewsconfig/type/{type}'
                                    , visible: function (row) {
                                        if(row.type == '5')
                                            return true;
                                        return false;
                                    }
                                },
                                {
                                    name: '内容管理'
                                    , text: __('内容管理')
                                    , title: __('内容管理')
                                    , classname: 'btn btn-xs btn-default btn-addtabs'
                                    , url: 'prl/articles/index'
                                    , visible: function (row) {
                                        if(row.type == '2')
                                            return true;
                                        return false;
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $(document).on("change", "form input[name='row[name]']", function (e) {
                $("#c-seo_title").val($(this).val() + '-' + Config.site.name);
                $("#c-seo_keywords").val($(this).val() + '-' + Config.site.name);
                $("#c-seo_description").val($(this).val() + '-' + Config.site.name);
            })
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                //不可见的元素不验证
                $("form").data("validator-options", {ignore: ':hidden'});
                
                $(document).on("change", "form select[name='row[type]']", function (e) {
                    var type = $(this).val();
                    $("#c-models_id").parents('.form-group').toggleClass("hide", ['0', '1', '4', '5', '6'].indexOf(type) > -1 ? true : false);
                    $("#c-pagesize").parents('.form-group').toggleClass("hide", ['0', '1', '4', '5', '6'].indexOf(type) > -1 ? true : false);
                    $("#c-home_view_id").parents('.form-group').toggleClass("hide", ['1', '2', '4', '5', '6'].indexOf(type) > -1 ? true : false);
                    $("#c-channel_view_id").parents('.form-group').toggleClass("hide", ['0', '2', '4', '5', '6'].indexOf(type) > -1 ? true : false);
                    $("#c-list_view_id").parents('.form-group').toggleClass("hide", ['0', '1', '4', '5', '6'].indexOf(type) > -1 ? true : false);
                    $("#c-info_view_id").parents('.form-group').toggleClass("hide", ['0', '1', '4', '5', '6'].indexOf(type) > -1 ? true : false);
                    $("#c-page_view_id").parents('.form-group').toggleClass("hide", ['0', '1', '2', '5', '6'].indexOf(type) > -1 ? true : false);
                    $("#c-href").parents('.form-group').toggleClass("hide", ['0', '1', '2', '4', '5'].indexOf(type) > -1 ? true : false);
                    $("#c-form_view_id").parents('.form-group').toggleClass("hide", ['0', '1', '2', '4', '6'].indexOf(type) > -1 ? true : false);
                    $("#c-form_id").parents('.form-group').toggleClass("hide", ['0', '1', '2', '4', '6'].indexOf(type) > -1 ? true : false);
                });

                $("#c-models_id").change(function(){
                    var id = $("#c-models_id").val();
                    $.post('prl/channels/getModelsViews',{id:id},function(e){
                        console.log(e.data.list_view_id);
                        $("#c-list_view_id").val(e.data.list_view_id).selectPageRefresh();
                        $("#c-info_view_id").val(e.data.info_view_id).selectPageRefresh();
                    })
                });

                //绑定事件
                Form.api.bindevent($("form[role=form]"));
                //给input设置一个宽度 否则显示的宽度150px
                $(".selectpage").css('width', $(".selectpage").parent('div').width());
                //触发选中
                $("form select[name='row[type]']").trigger("change");
            }
        },
        viewsconfig: function() {
            Form.api.bindevent($("form.edit-form"));
        }
    };
    return Controller;
});